<!doctype html>
<head>
    <title>Threebox Object3D Example</title>
    <script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
    <link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
    <script src="../dist/threebox.js" type="text/javascript"></script>
    <link href="../dist/threebox.css" rel="stylesheet" />
    <script src="config.js"></script>
    <style>
        body, html {
            width: 100%;
            height: 100%;
            margin: 0;
        }

        #map {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <div id='map' class='map'></div>

	<script type="module">
		if (!config) console.error("Config not set! Make a copy of 'config_template.js', add in your access token, and save the file as 'config.js'.");

		mapboxgl.accessToken = config.accessToken;
		var origin = [-122.4340, 37.7353, 0];

		var map = new mapboxgl.Map({
			container: 'map',
			style: 'mapbox://styles/mapbox/light-v9',
			center: origin,
			zoom: 12.5,
			pitch: 60,
			bearing: 80
		});


		// we can add Threebox to mapbox to add built-in mouseover/mouseout and click behaviors
		window.tb = new Threebox(
			map,
			map.getCanvas().getContext('webgl'),
			{
				defaultLights: true,
			}
		);
		let stats;
		import Stats from 'https://threejs.org/examples/jsm/libs/stats.module.js';
		function animate() {
			requestAnimationFrame(animate);
			stats.update();
		}

		map.on('style.load', function () {
			// stats
			stats = new Stats();
			map.getContainer().appendChild(stats.dom);
			animate();

			map.addLayer({
				id: 'custom_layer',
				type: 'custom',
				renderingMode: '3d',
				onAdd: function (map, mbxContext) {

					// import soldier from an external glb file, scaling up its size 20x
					// IMPORTANT: .glb is not a standard MIME TYPE, you'll have to add it to your web server config,
					// otherwise you'll receive a 404 error

                    // Attribution: Soldier animated model by T. Choonyung at https://www.mixamo.com
					// from https://www.mixamo.com/#/?page=1&query=vanguard&type=Character
					var options = {
						obj: './models/soldier.glb',
						type: 'gltf',
						scale: 1000,
						units: 'meters',
						rotation: { x: 90, y: 0, z: 0 }, //default rotation
						anchor: 'center'
					}

					tb.loadObj(options, function (model) {
						let soldier = model.setCoords(origin);
						tb.add(soldier);
					})

				},

				render: function (gl, matrix) {
					tb.update();
				}
			});

		});


	</script>
</body>